package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.Shift;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Description: 设备转移记录业务
 * @author: Rock
 * @time: 2020/10/29 17:30
 */
@Service
public class ShiftService {

    @Resource(name = "appMongoTemplate")
    private MongoTemplate mongoTemplate;

    /**
     * @param page
     * @param newImei
     * @time: 2020/10/29 17:33
     * @author: Rock
     * @version 1.0
     */
    public Page<Shift> list(PageVO page, String newImei) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("maketime")));
        pageable.setPage(page);

        Criteria criteria = new Criteria();
        if (StringUtils.isNotEmpty(page.getSearchValue())) {
            criteria.and("old_device").regex(".*?" + page.getSearchValue() + ".*?", "i");
        }
        if (StringUtils.isNotEmpty(newImei)) {
            criteria.and("new_device").regex(".*?" + newImei + ".*?", "i");
        }
        Query query = new Query().addCriteria(criteria);
        long count = mongoTemplate.count(query, Shift.class);
        page.setTotalResult((int) count);
        List<Shift> list = mongoTemplate.find(query.with(pageable), Shift.class);
        Page<Shift> result = new PageImpl<Shift>(list, pageable, count);
        return result;
    }

    /**
     * @param username
     * @param oldDevice
     * @param oldVideoEndtime
     * @param oldVideoPermanent
     * @param newDevice
     * @param newVideoEndtime
     * @param newVideoPermanent
     * @param updateVideoEndtime
     * @time: 2020/10/30 8:47
     * @author: Rock
     * @version 1.0
     */
    public void save(String username, String oldDevice, Integer oldVideoEndtime, Integer oldVideoPermanent,
                     String newDevice, Integer newVideoEndtime, Integer newVideoPermanent, Integer updateVideoEndtime) {
        Shift shift = new Shift();
        shift.setUsername(username);
        shift.setOldDevice(oldDevice);
        shift.setOldVideoEndtime(oldVideoEndtime);
        if (oldVideoPermanent != null) {
            shift.setOldVideoPermanent(oldVideoPermanent);
        }
        shift.setNewDevice(newDevice);
        shift.setNewVideoEndtime(newVideoEndtime);
        if (newVideoPermanent != null) {
            shift.setNewVideoPermanent(newVideoPermanent);
        }
        shift.setUpdateVideoEndtime(updateVideoEndtime);
        shift.setMaketime(new Date());
        shift.setAgent(8888);
        mongoTemplate.save(shift);
    }

}
